(function($) {
    Sw.Namespace("$.sw");
    $.fn.extend({
        accordion: function(options) {
            return this.each(function() {
                new $.sw.accordion(this, options);
            });
        }
    });
    $.sw.accordion = Class({
        container: null,
        options: null,
        items: null,
        isRendered: false,
        initialize: function(container, options) {
            this.container = $(container);
            this.options = $.extend({}, $.sw.accordion.defaults, options);
            this.items = this.options.items;
            this.initComponent();
        },
        initComponent: function() {
            if (this.items.length > 0 && !this.isRendered) {
                $(this.container).addClass(this.options.className).empty();
                var me = this;
                var aheader, atitle, aicon, acontent;
                $(this.items).each(function(index, item) {
                    aheader = $("<div/>").addClass(me.options.headerClass).appendTo(me.container);
                    atitle = $("<h2/>").html(item.title).appendTo(aheader);
                    aicon = $("<span/>").addClass(item.className).prependTo(atitle);
                    acontent = $("<div/>").addClass(me.options.contentClass).html(item.html).appendTo(me.container);
                    aheader = atitle = aicon = acontent = null;
                });
                //set height
                if (this.options.parent) {

                    this.setHeight();
                }
                else (this.options.autoheight)
                {
                    var maxHeight = 0;
                    $("." + this.options.contentClass, this.container).each(function() {
                        maxHeight = Math.max(maxHeight, $(this).outerHeight());
                    }).height(maxHeight);
                }
                //set active
                $("." + this.options.headerClass, this.container).each(function(index, header) {
                    if (me.options.active != index) {
                        $(header).next().hide();
                    }
                    else {
                        $(header).find("h2").addClass(me.options.selectedClass);
                    }
                });
                //set event
                $("." + this.options.headerClass, this.container).each(function(index, header) {
                    $(this).click(function() {
                        $("." + me.options.contentClass, me.container).hide().parent().find("h2").removeClass(me.options.selectedClass);
                        $(this).find("h2").addClass(me.options.selectedClass).andSelf().next().show();                 
                    }).hoverClass(me.options.hoverClass);
                });
                
            }
        },
        setHeight: function() {
            var parentContainer = $(this.container).parent();
            var csize = Sw.Size.fromElement(parentContainer, 1);
            var hsize = Sw.Size.fromElement($("." + this.options.headerClass, this.container).first(), 2);
            var tempBody = $("." + this.options.headerClass, this.container).first();
            var borderH = ($(tempBody).outerHeight() - $(tempBody).height()) * this.items.length;
            var height = csize.height - hsize.height * this.items.length - borderH;
            var os = parentContainer.children().not(this.container);
            $.each(os, function(i, element) {
                height = height - $(element).outerHeight();
            });
            $("." + this.options.contentClass, this.container).height(height - 2);
            csize = hsize = tempBody = borderH = height = os = null;
        }
    });
    $.extend($.sw.accordion, {
        defaults: {
            selectedClass: "collapsable",
            className: "sw-accordion",
            headerClass: "sw-accordion-header",
            contentClass: "sw-accordion-content",
            hoverClass: "hover",
            parent: true,
            autoheight: true,
            active: 0
           
        }
    });
})(jQuery);
